<html>
<head><meta charset="utf-8"><title>generate and execute wasm on the fly · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html">generate and execute wasm on the fly</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="197800056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197800056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> zeroexcuses <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197800056">(May 16 2020 at 15:11)</a>:</h4>
<p>Is there any Rust crate that makes it possible to (inside a Rust compiled to wasm application) to generate wasm and execute it on the fly?</p>



<a name="197801763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197801763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197801763">(May 16 2020 at 15:50)</a>:</h4>
<p>WebAssembly doesn't allow dynamic code generation like that. There has occasionally been discussion of the ability to do so, to support things like JITs, but currently the standard doesn't allow it.</p>



<a name="197801784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197801784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197801784">(May 16 2020 at 15:51)</a>:</h4>
<p>What goal do you have, that doing so would have helped with? Perhaps there's another way to accomplish it?</p>



<a name="197804054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197804054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> zeroexcuses <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197804054">(May 16 2020 at 16:46)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span>  : Thanks. This is really helpful. Can you point me to the wasm standard discussions on dynamic code generation + execution vs no ?</p>
<p>My particular use case involves an APL/J/K interpreter. There are some operations where one can do 'stream fusion' type operations to reduce the amount of memory bandwidth one uses (but at the cost of more complicated 'kernels' running per 'cell'). Right now, in these 'kernels', there is a giant switch statement that is executed 'per instruction' ... whereas I would prefer to just run through the giant switch once, convert it to wasm/wast, and then execute it without the overhead of "one switch statement per instruction"</p>



<a name="197806842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197806842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197806842">(May 16 2020 at 17:51)</a>:</h4>
<p><span class="user-mention" data-user-id="299970">@zeroexcuses</span> I don't know, offhand, of a reference to point you to. However, if you're interested in adding such capabilities, you might talk to the Bytecode Alliance (disclaimer: I'm a member), which works on software and interfaces for WebAssembly in various environments (including non-browser environments). I could imagine pursuing, through there, an interface for providing "dynamically generated WASM modules" and being able to link them in on the fly, the equivalent of <code>dlopen</code>. That might help.</p>



<a name="197834112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197834112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197834112">(May 17 2020 at 06:44)</a>:</h4>
<p>This is a very interesting discussion! I’m also interested in exploring my own small language for data transformations, which in the end shall be executed from native Rust code (no WASM) with good efficiency. Bundling rustc and using <code>dlopen</code> is a possibility, but this thread made me wonder whether there are more light-weight alternatives.</p>



<a name="197878182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197878182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> zeroexcuses <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197878182">(May 18 2020 at 00:49)</a>:</h4>
<p><span class="user-mention" data-user-id="302838">@Roland Kuhn</span>  : Are you able to get around the issue of "one match per instruction executed" ? This is the problem I am running into with a pure rust solution:</p>
<ol>
<li>I need to define some "pub enum VMInstr { ... }" , which is the set of 'primitive instructions'</li>
<li>whatever higher level ops gets 'compiled down' to VMInstr</li>
<li>the problem is taht when executing a <code>Vec&lt;VMInstr&gt;</code>, we have to do a match on evern VMInstr, per instruction executed. I have not benchmarked this, but this seems horribly inefficient.</li>
</ol>



<a name="197892384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197892384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197892384">(May 18 2020 at 06:10)</a>:</h4>
<p><span class="user-mention" data-user-id="299970">@zeroexcuses</span> I should have been a bit clearer: I’m not yet actively pursuing this, in part because other things need to get done first and in part because I have not yet been able to think of a suitable design. A long time ago I wrote an expression evaluator in Pascal, which had the same issue you described; the solution back then was to include one assembler-written function that can just call a raw function pointer. With all the Rust infrastructure, maybe this can be simplified to compiling to a <code>Vec&lt;Box&lt;dyn VMInstr&gt;&gt;</code> (i.e. going from an enum to a trait), trading the huge switch for one memory indirection, which should improve performance in the “huge enough switch” case.</p>
<p>My question was aimed at whether there is some other tooling we could use to not only get rid of either overhead, but also apply optimizations suitable for the hardware that executes all this — where running rustc is a (very heavy) solution AFAICS.</p>



<a name="197893042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generate%20and%20execute%20wasm%20on%20the%20fly/near/197893042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generate.20and.20execute.20wasm.20on.20the.20fly.html#197893042">(May 18 2020 at 06:21)</a>:</h4>
<p>"Computed goto" can help with that, but I don't think it's possible in Rust. Anyway, a big switch is what most interpreted languages do and it works fine for them. If you want better performance, you can add more primitives to it (e.g. array/vectorized operations).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>